﻿#include "C3fdFlowPhysicalControl.h"
#include "ui_C3fdFlowPhysicalControl.h"
#include "C3fdChartWidget.h"
#include "C3fdRenderWidget.h"
#include <vtkDataSet.h>
#include <vtkFieldData.h>
#include <vtkDataArray.h>

namespace MainWidget
{

	C3fdFlowPhysicalControl::C3fdFlowPhysicalControl(QWidget *buddies, QString type, QWidget *parent)
		: QWidget(parent), m_Ui(new Ui::C3fdFlowPhysicalControl), m_BuddiesWidget(buddies), m_Type(type)
	{
		m_Ui->setupUi(this);
		initContent();
	}

	C3fdFlowPhysicalControl::~C3fdFlowPhysicalControl()
	{
		m_Ui->lw_FlowPhysicalList->clear();
		m_BuddiesWidget = nullptr;
		delete m_Ui;
	}

	void C3fdFlowPhysicalControl::initContent()
	{
		if (m_BuddiesWidget == nullptr || m_Type.isEmpty())
		{
			return;
		}

		if (m_Type.toLower() == "qwt")
		{
			m_Ui->lw_FlowPhysicalList->clear();
			auto *widget = dynamic_cast<C3fdChartWidget *>(m_BuddiesWidget);
			if (widget == nullptr)
			{
				return;
			}
			auto seriesTitles = widget->getSeriesTitles();

			for (auto s : seriesTitles)
			{
				auto *item = new QTreeWidgetItem(m_Ui->lw_FlowPhysicalList);
				item->setText(0, translateFieldName(s));
				m_Ui->lw_FlowPhysicalList->addTopLevelItem(item);
			}
		}
		else if (m_Type.toLower() == "vtk")
		{
			m_Ui->lw_FlowPhysicalList->clear();

			auto *widget = dynamic_cast<C3fdRenderWidget *>(m_BuddiesWidget);
			if (widget == nullptr)
			{
				return;
			}

			auto fields = widget->getFields();
			for (auto k : fields.keys())
			{
				for (auto name : fields.value(k))
				{
					auto *item = new QTreeWidgetItem(m_Ui->lw_FlowPhysicalList);
					if (k == Common::FieldLocation::Point)
					{
						item->setIcon(0, QIcon(":/QUI/post/val_on_point.png"));
					}
					else if (k == Common::FieldLocation::Cell)
					{
						item->setIcon(0, QIcon(":/QUI/post/val_on_cell.png"));
					}
					item->setData(0, Qt::DisplayRole, translateFieldName(name));
					item->setData(0, Qt::UserRole, name);
					m_Ui->lw_FlowPhysicalList->addTopLevelItem(item);
				}
			}
		}
	}

	QString C3fdFlowPhysicalControl::translateFieldName(QString name)
	{
		if (name == "MACHabs")
			return tr("MACHabs");
		else if (name == "MACHrel")
			return tr("MACHrel");
		else if (name == "Vzabs")
			return tr("Vzabs");
		else if (name == "Vyabs")
			return tr("Vyabs");
		else if (name == "Vxrel")
			return tr("Vxrel");
		else if (name == "Vyrel")
			return tr("Vyrel");
		else if (name == "Vx")
			return tr("Vx");
		else if (name == "Entropy")
			return tr("Entropy");
		else if (name == "TTabs")
			return tr("TTabs");
		else if (name == "TPabs")
			return tr("TPabs");
		else if (name == "TPrel")
			return tr("TPrel");
		else if (name == "Spre")
			return tr("Spre");
		else if (name == "TPRE")
			return tr("TPRE");
		else if (name == "SPRE")
			return tr("SPRE");
		else if (name == "Mrel")
			return tr("Mrel");
		else if (name == "Mabs")
			return tr("Mabs");
		else if (name == "VCr")
			return tr("VCr");
		else if (name == "VCa")
			return tr("VCa");
		else if (name == "VR")
			return tr("VR");
		else if (name == "VX")
			return tr("VX");
		else if (name == "FTVS")
			return tr("FTVS");
		else if (name == "ENTROPY")
			return tr("ENTROPY");
		else if (name == "TTEM")
			return tr("TTEM");
		else if (name == "STEM")
			return tr("STEM");
		else if (name == "static pressure(Pa)")
			return tr("static pressure");
		else if (name == "Vzj2")
			return tr("Vzj2");
		else if (name == "Vyj2")
			return tr("Vyj2");
		else if (name == "Vxj2")
			return tr("Vxj2");
		else if (name == "Yplus")
			return tr("Yplus");
		else if (name == "SPRE")
			return tr("SPRE");
		else
			return name;
	}

	void C3fdFlowPhysicalControl::on_lw_FlowPhysicalList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
	{
		if (current == nullptr)
		{
			return;
		}
		if (m_Type.toLower() == "vtk")
		{
			auto *widget = dynamic_cast<C3fdRenderWidget *>(m_BuddiesWidget);
			if (widget == nullptr)
			{
				return;
			}
			widget->updateShadeField(current->data(0, Qt::UserRole).toString());
		}
	}
}
